package com.echat.elocation.util;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.GnssStatus;
import android.location.GpsStatus;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;

import com.echat.elocation.base.BaseApplication;

/**
 * GPS工具类
 *
 * @author lihao
 * @date 2018/7/16 11:10
 */
public class GpsUtils {

    private static LocationManager mLocationManager;

    public static Context getContext() {
        return BaseApplication.getContext();
    }

    public static LocationManager getLocationManager() {
        if (mLocationManager == null) {
            mLocationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
        }
        return mLocationManager;
    }

    public static boolean isOpenGps() {
        return getLocationManager().isProviderEnabled(LocationManager.GPS_PROVIDER);
    }

    public static void openGps(Activity activity, int code) {
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        activity.startActivityForResult(intent, code);
    }

    public static void startLocation(String provider, long minTime, float minDistance,
                                     LocationListener listener) {
        if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        getLocationManager().requestLocationUpdates(provider, minTime, minDistance, listener);
    }

    public static void stopLocation(LocationListener listener) {
        getLocationManager().removeUpdates(listener);
    }

    /**
     * 注册GPS状态监听器，根据API的大小调用不同的监听者，不需要的监听者可设置为空
     * @param listener API < 24时调用
     * @param callback API >= 24时调用
     */
    public static void registerGnssStatusCallback(GpsStatus.Listener listener, GnssStatus.Callback callback) {
        if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.N){
            checkListener(listener);
            getLocationManager().addGpsStatusListener(listener);
        } else {
            checkCallback(callback);
            getLocationManager().registerGnssStatusCallback(callback);
        }
    }

    /**
     * 移除GPS状态监听器，根据API的大小调用不同的监听者，不需要的监听者可设置为空
     * @param listener API < 24时调用
     * @param callback API >= 24时调用
     */
    public static void unregisterGnssStatusCallback(GpsStatus.Listener listener, GnssStatus.Callback callback){
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.N){
            checkListener(listener);
            getLocationManager().removeGpsStatusListener(listener);
        } else {
            checkCallback(callback);
            getLocationManager().unregisterGnssStatusCallback(callback);
        }
    }

    private static void checkListener(GpsStatus.Listener listener){
        if(listener == null){
            throw new IllegalArgumentException("invalid listener: " + listener);
        }
    }

    private static void checkCallback(GnssStatus.Callback callback){
        if(callback == null){
            throw new IllegalArgumentException("invalid callback: " + callback);
        }
    }

}
